package com.zmj.sy.mom.srv.aps.utils;

import com.zmj.sy.mom.srv.aps.bean.entity.mes.*;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.Position;
import com.zmj.sy.mom.srv.aps.mapper.*;
import com.zmj.sy.mom.srv.aps.service.PartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@RequiredArgsConstructor
@Component
public class PlateUtils {

     private final PartService partService;
    private final PositionMapper positionMapper;
    private final WorkDetailTaskMapper workDetailTaskMapper;
    private final PartPalletMapper partPalletMapper;
    private final PartLogMapper partLogMapper;
    private final PlatePhaseMapper platePhaseMapper;

    /**
     * 钢板创建
     * 1、钢板出库  2、钢板报工
     * @param plateCode
     * @param phaseCode
     * @param phaseName
     * @param prePhaseCode
     * @param workChain
     * @param nextgrouop
     */
    public Part plateUpdate(String plateCode, String phaseCode, String phaseName, String prePhaseCode, String workChain,String nextgrouop) {

        Part one =partService.getPart(1,plateCode);

        // 判断零件库中是否已存在
        if(one == null){
            one = new Part();
            one.setPartType(1);
            one.setPlmId(plateCode);
            one.setPrePhaseCode(prePhaseCode);
            one.setPhaseCode(phaseCode);
            one.setPhaseName(phaseName);
            one.setWorkChain(workChain);
            one.setWorkGroup(nextgrouop);
            one.setFailCount(0);
            one.setPartCount(1);
            partService.creatPart(one);
        } else {
            one.setWorkGroup(nextgrouop);
            one.setPrePhaseCode(prePhaseCode);
            one.setPhaseCode(phaseCode);
            one.setPhaseName(phaseName);
            partService.updateById(one);
        }


        // 如果钢板信息为空的时候，判断是否已经存在，如果存在则抛异常
        PartPallet pp = partPalletMapper.lambdaQuery().eq(PartPallet::getPlmId, one.getPlmId()).one();
        if(pp != null){
            pp.setPrePhaseCode(one.getPrePhaseCode());
            pp.setPhaseCode(one.getPhaseCode());
            partPalletMapper.updateById(pp);
        }
        return one;


    }

    public WorkDetailTask makePlateTask(Plate plate,  WorkDetailTask task , int phaseIndex,String[] split ) {
        //       phaseIndex  数组下标  开始为0
        PlatePhase phase = platePhaseMapper.lambdaQuery().eq(PlatePhase::getPlateCode, plate.getMaterCode()).eq(PlatePhase::getPhaseSeq, phaseIndex + 2).one();
        WorkDetailTask plateTask = new WorkDetailTask();
        plateTask.setPhaseCode(phase.getPhaseCode());
        plateTask.setPlmid(plate.getMaterCode());
        plateTask.setPlateCode(plate.getMaterCode());
        plateTask.setPhaseName(phase.getPhaseName());
        plateTask.setGroupCode(phase.getGroupCode());
        plateTask.setType(1);
        plateTask.setMaterCode(plate.getMaterCode());
        plateTask.setMaterName(plate.getMaterName());
        plateTask.setStatus(1);
        plateTask.setPlanStartDateTime(plate.getNestDateTime());
        plateTask.setPlanEndDateTime(plateTask.getPlanStartDateTime().plusHours(2));
        plateTask.setOrderCode(task.getOrderCode());
        plateTask.setNorm(plate.getNorm());
        plateTask.setTexture(plate.getTexture());
        plateTask.setWeight(plate.getWeight());
        plateTask.setWidth(plate.getWidth());
        plateTask.setLength(plate.getLength());
        plateTask.setNestId(plate.getNestId());
        plateTask.setPartPath(plate.getNestingDrawing());
        plateTask.setNestPlateId(task.getNestPlateId());
        workDetailTaskMapper.insert(plateTask);
        return plateTask;
    }

/*    public void addPlatePallet(String plateCode, String phaseCode, String palletCode, String prePhaseCode){



        PartPallet nextPartPallet = new PartPallet();
        nextPartPallet.setPartCount(1);
        nextPartPallet.setPartType(1);
        nextPartPallet.setType(3);

        nextPartPallet.setPlmId(plateCode);
        nextPartPallet.setPhaseCode(phaseCode);
        nextPartPallet.setLoadingCode(palletCode);
        nextPartPallet.setPrePhaseCode(prePhaseCode);

        partPalletMapper.insert(nextPartPallet);
    }*/

    public void plateIn(Part platePart, String groupCode, String stationCode, String positionCode) {
        PartPallet one = partPalletMapper.lambdaQuery()
                .eq(PartPallet::getPlmId, platePart.getPlmId())
                .eq(PartPallet::getPartType, platePart.getPartType())
                .one();
        if(StringUtils.hasText(stationCode)&&(stationCode.contains("_ZDFJ_")||stationCode.contains("_RGFJ_"))){
            positionCode=stationCode+"#"+positionCode;
            try {
                PartPallet oldPart = partPalletMapper.lambdaQuery()
                        .eq(PartPallet::getPositionCode,positionCode)
                        .one();
                if(oldPart!=null){
                    Position pos = positionMapper
                            .lambdaQuery()
                            .eq(Position::getMyCode, oldPart.getPositionCode()).one();
                    if(pos.getSeq()==2){
                        Position threePos = positionMapper
                                .lambdaQuery()
                                .eq(Position::getStationId,pos.getStationId()).eq(Position::getSeq,3).one();
                        oldPart.setPositionCode(threePos.getMyCode());
                        partPalletMapper.updateById(oldPart);
                    }else if(pos.getSeq()==1){
                        Position twoPos = positionMapper
                                .lambdaQuery()
                                .eq(Position::getStationId,pos.getStationId()).eq(Position::getSeq,2).one();
                        Position threePos = positionMapper
                                .lambdaQuery()
                                .eq(Position::getStationId,pos.getStationId()).eq(Position::getSeq,3).one();
                        PartPallet twoPart = partPalletMapper.lambdaQuery()
                                .eq(PartPallet::getPositionCode,twoPos.getMyCode())
                                .one();
                        if(twoPart!=null){
                            twoPart.setPositionCode(threePos.getMyCode());
                            partPalletMapper.updateById(twoPart);
                        }
                        oldPart.setPositionCode(twoPos.getMyCode());
                        partPalletMapper.updateById(oldPart);
                    }

                }
            } catch (Exception e) {

            }
        }
        if(one == null){
            PartPallet partPallet = new PartPallet();
            partPallet.setPartCount(1);
            partPallet.setPartType(1);
            if(StringUtils.hasText(positionCode)){
                partPallet.setType(2);
                partPallet.setPositionCode(positionCode);
            } else if (StringUtils.hasText(stationCode)){
                partPallet.setType(3);
                partPallet.setPositionCode(stationCode);
            }
            partPallet.setPlmId(platePart.getPlmId());
            partPallet.setPhaseCode(platePart.getPhaseCode());
            partPallet.setPrePhaseCode(platePart.getPrePhaseCode());
            partPalletMapper.insert(partPallet);
        } else {
            one.setPositionCode(stationCode);
            if(StringUtils.hasText(positionCode)){
                one.setType(2);
                one.setPositionCode(positionCode);
            } else if (StringUtils.hasText(stationCode)){
                one.setType(3);
                one.setPositionCode(stationCode);
            }
            one.setPhaseCode(platePart.getPhaseCode());
            one.setPrePhaseCode(platePart.getPrePhaseCode());
            partPalletMapper.updateById(one);
        }
        // 添加操作记录
      /*  PartLog ph = new PartLog();
        ph.setOperationType(5);
        ph.setPartType(1);
        ph.setPartId(platePart.getPlmId());
        ph.setPhaseCode(platePart.getPrePhaseCode());
        ph.setNextPhaseCode(platePart.getPhaseCode());
        ph.setTotalCount(1);
        ph.setUniCode(null);
        ph.setPrePartHistory(null);
        ph.setLoadType(2);
        ph.setLoadId(stationCode);
        ph.setUserCode(SySecurityUtils.getUserCode());
        partLogMapper.insert(ph);
*/

    }

    public void plateOut(Part platePart, String groupCode, String stationCode) {
        PartPallet one = partPalletMapper.lambdaQuery()
                .eq(PartPallet::getPlmId, platePart.getPlmId())
                .eq(PartPallet::getPartType, platePart.getPartType())
                .one();

        partPalletMapper.deleteReal(one.getId());
        // 添加操作记录
       /* PartLog ph = new PartLog();
        ph.setOperationType(6);
        ph.setPartType(1);
        ph.setPartId(platePart.getPlmId());
        ph.setPhaseCode(platePart.getPrePhaseCode());
        ph.setNextPhaseCode(platePart.getPhaseCode());
        ph.setTotalCount(1);
        ph.setUniCode(null);
        ph.setPrePartHistory(null);
        ph.setLoadType(2);
        ph.setLoadId(one.getLoadingCode());
        ph.setUserCode(SySecurityUtils.getUserCode());
        partLogMapper.insert(ph);*/
    }

    public void clearPlate(String plateCode) {

        partService.clearPlate(plateCode);
        List<PartPallet> pps = partPalletMapper.lambdaQuery()
                .eq(PartPallet::getPlmId,plateCode)
//                .eq(PartPallet::getPhaseCode, WorkshopProp.FJ_PHASE_CODE)
                .eq(PartPallet::getPartType, 1)
                .list();
        if (!CollectionUtils.isEmpty(pps)) {
            for (PartPallet pp : pps){
                partPalletMapper.deleteReal(pp.getId());
            }
        }

       /* PartLog ph = new PartLog();
        ph.setOperationType(7);
        ph.setPartType(1);
        ph.setPartId(part.getPlmId());
        ph.setPhaseCode(part.getPrePhaseCode());
        ph.setNextPhaseCode(part.getPhaseCode());
        ph.setTotalCount(1);
        ph.setUniCode(null);
        ph.setPrePartHistory(null);
        ph.setLoadType(2);
        ph.setLoadId(partPallet.getLoadingCode());
        ph.setUserCode(SySecurityUtils.getUserCode());
        partLogMapper.insert(ph);*/
    }

    public void createPartLog(WorkRecord workRecord,String nextPhaseCode,int OperationType,String plateCode) {
        PartLog ph = new PartLog();
        ph.setOperationType(OperationType);
        ph.setPartType(1);
        ph.setPartId(workRecord.getPlmid());
        ph.setPhaseCode(workRecord.getPhaseCode());
        ph.setNextPhaseCode(nextPhaseCode);
        ph.setTotalCount(1);
        ph.setSuccessCount(1);
        ph.setErrorCount(0);
        ph.setUniCode(workRecord.getPlmid());
        ph.setPrePartHistory(null);
        ph.setLoadType(0);
        ph.setLoadId(plateCode);
        ph.setGroupCode(workRecord.getGroupNumber());
        ph.setStationCode(workRecord.getStationCode());
        ph.setUserCode(workRecord.getWorkStaffCode());
        ph.setReportId(workRecord.getId());
        partLogMapper.insert(ph);
    }

    public void createPartLog(Part newPart,String nextgrouop,String stationCode,int OperationType,String plateCode,String userCode) {
        PartLog ph = new PartLog();
        ph.setOperationType(OperationType);
        ph.setPartType(1);
        ph.setPartId(newPart.getPlmId());
        ph.setPhaseCode(newPart.getPrePhaseCode());
        ph.setNextPhaseCode(newPart.getPhaseCode());
        ph.setTotalCount(1);
        ph.setSuccessCount(1);
        ph.setErrorCount(0);
        ph.setUniCode(newPart.getPlmId());
        ph.setPrePartHistory(null);
        ph.setLoadType(0);
        ph.setLoadId(plateCode);
        ph.setGroupCode(nextgrouop);
        ph.setStationCode(stationCode);
        ph.setUserCode(userCode);
        partLogMapper.insert(ph);
    }

    public void deletePartLog(String palateCode) {

    }
}
